package controllers;

import java.util.Date;
import java.util.List;

import models.Business;
import models.BusinessIndicator;
import models.CoreTable;
import models.User;

import org.apache.commons.lang.StringUtils;

import play.data.validation.Required;
import utils.Paged;

public class BusinessIndicatorController extends BaseController {
    
    private static String createQueryHql(String name, Long businessId, Long authorId) {
        StringBuilder hql = new StringBuilder(" 1 = 1 ");
        if (!StringUtils.isBlank(name)) {
            hql.append(" and name like '%").append(name).append("%' ");
        }
        if (businessId != null) {
            hql.append(" and business.id = '").append(businessId).append("'");
        }
        if (authorId != null) {
            hql.append(" and author.id = '").append(authorId).append("'");
        }
        return hql.toString();
    }
    
    public static void list(String name, Long businessId, Long authorId, int currentPage) {
        String hql = createQueryHql(name, businessId, authorId);
        currentPage = getCurrentPage(currentPage);
        int count = new Long(BusinessIndicator.count(hql)).intValue();
        List<BusinessIndicator> tables = BusinessIndicator.find(hql).from((currentPage - 1) * PAGE_SIZE).fetch(PAGE_SIZE);
        Paged<BusinessIndicator> paged = new Paged<BusinessIndicator>(tables, currentPage, PAGE_SIZE, count);
        List<User> authors =  User.findActiveUsers();
        List<Business> businesses = Business.findActiveBusinesses();
        render("/indicator/list.html", paged, name, businessId, authorId, authors, businesses);
    }
    
    public static void form(Long indicatorId) {
        List<User> authors = User.findActiveUsers();
        List<Business> businesses = Business.findActiveBusinesses();
        if (indicatorId == null) {
            render("/indicator/form.html", authors, businesses);
        }
        
        BusinessIndicator indicator = BusinessIndicator.findById(indicatorId);
        if (indicator == null) {
            notFound();
        }
        
        render("/indicator/form.html", indicator, authors, businesses);
    }
    
    public static BusinessIndicator toSetIndicatorValue(BusinessIndicator indicator, Long businessId, Long authorId, String name, String description, String remark) {
        indicator.author = new User(authorId);
        indicator.business = new Business(businessId);
        indicator.name = name;
        indicator.description = description;
        indicator.remark = remark;
        indicator.updateDate = new Date();
        return indicator;
    }
    
    
    
    public static void save(@Required(message = "indicator.business.require")Long businessId,
                            @Required(message = "indicator.author.require") Long authorId, 
                            @Required(message = "indicator.name.require") String name, 
                            String description, String remark) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(null);
        }
        
        BusinessIndicator indicator = new BusinessIndicator();
        indicator = toSetIndicatorValue(indicator, businessId, authorId, name, description, remark).save();
        form(indicator.id);
    }
    
    public static void update(Long indicatorId,
                            @Required(message = "indicator.business.require")Long businessId,
                            @Required(message = "indicator.author.require") Long authorId, 
                            @Required(message = "indicator.name.require") String name, 
                            String description, String remark) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(indicatorId);
        }
        BusinessIndicator indicator = BusinessIndicator.findById(indicatorId);
        if (indicator == null ) {
            notFound();
        }
        indicator = toSetIndicatorValue(indicator, businessId, authorId, name, description, remark).save();
        form(indicator.id);
    }
    
    public static void delete (Long indicatorId) {
        BusinessIndicator.delete("id", indicatorId);
        list(null, null, null, FIRST_PAGE);
    }
}